package com.demo.javaDemo.data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public abstract class CacheIterator<E> implements Iterator<E> {
	Iterator<E> cacheData=null;
	int pageSize=5;
	int pageLen=10;
	int pageIndex=0;
	public static void main(String[] args) {
		Iterator<String> iter=new CacheIterator<String>() {
			@Override
			public Iterator<String> fetch() {
				List<String> dd=new ArrayList<>();
				if(pageIndex++<pageLen) {
					for(int i=0;i<pageSize;i++) {
						dd.add(pageIndex+"-"+i);
					}
				}
				return dd.iterator();
			}
		};
		while(iter.hasNext()) {
			System.out.println(iter.next());
		}
	}
	public abstract Iterator<E> fetch();
 	
	@Override
	public boolean hasNext() {
		if(cacheData==null||!cacheData.hasNext()) {
			cacheData= fetch();
		}
		return cacheData.hasNext();
	}

	@Override
	public E next() {
		return cacheData.next();
	}

}
